package com.flutter.jl_plugin.bluetooth;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattService;
import android.os.Handler;
import android.os.Looper;
import android.os.ParcelUuid;
import android.util.Base64;
import android.util.Log;

import com.flutter.jl_plugin.JlPlugin;
import com.google.gson.Gson;
import com.jieli.bluetooth.bean.BleScanMessage;
import com.jieli.bluetooth.bean.VoiceData;
import com.jieli.bluetooth.bean.base.BaseError;
import com.jieli.bluetooth.bean.base.CommandBase;
import com.jieli.bluetooth.bean.command.custom.CustomCmd;
import com.jieli.bluetooth.bean.parameter.CustomParam;
import com.jieli.bluetooth.constant.Command;
import com.jieli.bluetooth.constant.StateCode;
import com.jieli.bluetooth.impl.rcsp.RCSPController;
import com.jieli.bluetooth.interfaces.bluetooth.IBluetoothCallback;
import com.jieli.bluetooth.utils.CHexConver;
import com.jieli.bluetooth.utils.CommandBuilder;
import com.jieli.bluetooth.utils.JL_Log;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class SppBlueToothCallBack implements IBluetoothCallback {
    String TAG="SppBlueToothCallBack";
    Handler handler=new Handler(Looper.getMainLooper());
    @Override
    public void onDeviceUuidsDiscovery(BluetoothDevice bluetoothDevice, ParcelUuid[] parcelUuids) {

    }

    @Override
    public void onSppDataNotification(BluetoothDevice device, byte[] bytes) {
        CommandBase cmd = CommandBuilder.buildCustomCmd(bytes);
        if (cmd.getId() == Command.CMD_EXTRA_CUSTOM) {
            Map<String, Object> map = new HashMap<>();

            CustomCmd customCmd= (CustomCmd) cmd;
            CustomParam param = customCmd.getParam();
            if(param==null){
                return;
            }
            byte[] data = param.getData();
            if(data.length==52&&data[9]==0x07&&(data[10] & 0xFF) == 0xFE){
                byte[] subData = Arrays.copyOfRange(data, 9, data.length);
                String receiveCmd=CHexConver.byte2HexStr(subData);
                JL_Log.e(TAG,"接收到设备发送的自定义数据"+receiveCmd);
                map.put("address",device.getAddress());
                map.put("name",device.getName());
                Map<String, Object> cmdMap = new HashMap<>();
                cmdMap.put("type",cmd.getType());
                cmdMap.put("receiveCmd",receiveCmd);
                map.put("cmd",cmdMap);
                JlPlugin.Companion.getChannel().invokeMethod("onDeviceCommand",map);
            }


        }
    }




    @Override
    public void onBleServiceDiscovery(BluetoothDevice bluetoothDevice, int i, List<BluetoothGattService> list) {

    }

    @Override
    public void onBleConnection(BluetoothDevice bluetoothDevice, int i) {

    }

    @Override
    public void onBleDataBlockChanged(BluetoothDevice bluetoothDevice, int i, int i1) {

    }

    @Override
    public void onBleNotificationStatus(BluetoothDevice bluetoothDevice, UUID uuid, UUID uuid1, boolean b) {

    }

    @Override
    public void onBleDataNotification(BluetoothDevice bluetoothDevice, UUID uuid, UUID uuid1, byte[] bytes) {

    }

    @Override
    public void onBleWriteStatus(BluetoothDevice bluetoothDevice, UUID uuid, UUID uuid1, byte[] bytes, int i) {

    }

    @Override
    public void onBtDeviceConnectStatus(BluetoothDevice bluetoothDevice, int i) {

    }

    @Override
    public void onConnectionUpdated(BluetoothGatt bluetoothGatt, int i, int i1, int i2, int i3) {

    }

    @Override
    public void onBrEdrConnection(BluetoothDevice bluetoothDevice, int i) {

    }

    @Override
    public void onAdapterStatus(boolean b, boolean b1) {

    }

    @Override
    public void onDiscoveryStatus(boolean b, boolean b1) {

    }

    @Override
    public void onDiscovery(BluetoothDevice bluetoothDevice) {

    }

    @Override
    public void onDiscovery(BluetoothDevice bluetoothDevice, BleScanMessage bleScanMessage) {

    }

    @Override
    public void onShowDialog(BluetoothDevice bluetoothDevice, BleScanMessage bleScanMessage) {

    }

    @Override
    public void onBondStatus(BluetoothDevice bluetoothDevice, int i) {

    }

    @Override
    public void onConnection(BluetoothDevice bluetoothDevice, int i) {

    }

    @Override
    public void onSwitchConnectedDevice(BluetoothDevice bluetoothDevice) {

    }

    @Override
    public void onA2dpStatus(BluetoothDevice bluetoothDevice, int i) {

    }

    @Override
    public void onHfpStatus(BluetoothDevice bluetoothDevice, int i) {

    }

    @Override
    public void onSppStatus(BluetoothDevice bluetoothDevice, int i) {

    }

    @Override
    public void onDeviceCommand(BluetoothDevice bluetoothDevice, CommandBase commandBase) {

    }

    @Override
    public void onDeviceData(BluetoothDevice bluetoothDevice, byte[] bytes) {

    }

    @Override
    public void onDeviceVoiceData(BluetoothDevice bluetoothDevice, byte[] bytes) {

    }

    @Override
    public void onDeviceVoiceData(BluetoothDevice bluetoothDevice, VoiceData voiceData) {

    }

    @Override
    public void onDeviceVadEnd(BluetoothDevice bluetoothDevice) {

    }

    @Override
    public void onDeviceResponse(BluetoothDevice bluetoothDevice, CommandBase commandBase) {

    }

    @Override
    public void onError(BaseError baseError) {

    }
}
